package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JToggleButton;

import control.Alert;
import java.awt.Font;
import javax.swing.SwingConstants;

//@author A0081236L
public class AlertManagerPanel extends BackgroundPanel {
	
	private JLabel lblBalanceAlert;
	private JLabel lblDailyAlert;
	private JLabel lblWeeklyAlert;
	private JLabel lblMonthlyAlert;
	private JLabel lblYearlyAlert;
	private JLabel lblExpenditureAlert;	
	private JToggleButton jToggleBalanceAlert;
	private JToggleButton jToggleDailyAlert;
	private JToggleButton jToggleWeeklyAlert;
	private JToggleButton jToggleMonthlyAlert;
	private JToggleButton jToggleYearlyAlert;
	private JToggleButton jToggleExpenditureAlert;	
	private SidePane _sidePane;
	private Alert _alert;
	
	public AlertManagerPanel(SidePane sidePane, final Alert alert){
		setLayout(null);
		_sidePane = sidePane;
		_alert = alert;
		
		lblBalanceAlert = new JLabel("Balance Alert");
		lblBalanceAlert.setBounds(180, 67, 144, 14);
		this.add(lblBalanceAlert);
		
        jToggleBalanceAlert = new JToggleButton("Off");
		jToggleBalanceAlert.setBounds(108, 64, 60, 23);
		this.add(jToggleBalanceAlert);	
		jToggleBalanceAlert.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setBalance(alert);
				_sidePane.update();
			}
		});
		if(alert.getBalanceCheck())
		{
			jToggleBalanceAlert.setSelected(true);
			setBalance(alert);		
		}
		
		lblDailyAlert = new JLabel("Daily Budget Alert");
		lblDailyAlert.setBounds(180, 112, 144, 14);
		this.add(lblDailyAlert);
		
		jToggleDailyAlert = new JToggleButton("Off");
		jToggleDailyAlert.setBounds(108, 109, 60, 23);
		this.add(jToggleDailyAlert);	
		jToggleDailyAlert.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setDaily(alert);
				_sidePane.update();
			}
		});
		if(alert.getDailyBudgetCheck())
		{
			jToggleDailyAlert.setSelected(true);
			setDaily(alert);		
		}
		
		lblWeeklyAlert = new JLabel("Weekly Budget Alert");
		lblWeeklyAlert.setBounds(180, 162, 144, 14);
		this.add(lblWeeklyAlert);
		
		jToggleWeeklyAlert = new JToggleButton("Off");
		jToggleWeeklyAlert.setBounds(108, 159, 60, 23);
		this.add(jToggleWeeklyAlert);
		jToggleWeeklyAlert.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setWeekly(alert);
				_sidePane.update();
			}
		});
		if(alert.getWeeklyBudgetCheck())
		{
			jToggleWeeklyAlert.setSelected(true);
			setWeekly(alert);		
		}
		
		lblMonthlyAlert = new JLabel("Monthly Budget Alert");
		lblMonthlyAlert.setBounds(180, 212, 144, 14);
		this.add(lblMonthlyAlert);
		
		jToggleMonthlyAlert = new JToggleButton("Off");
		jToggleMonthlyAlert.setBounds(108, 209, 60, 23);
		this.add(jToggleMonthlyAlert);	
		jToggleMonthlyAlert.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setMonthly(alert);
				_sidePane.update();
			}
		});
		if(alert.getMonthlyBudgetCheck())
		{
			jToggleMonthlyAlert.setSelected(true);
			setMonthly(alert);		
		}
		
		lblYearlyAlert = new JLabel("Yearly Budget Alert");
		lblYearlyAlert.setBounds(180, 262, 144, 14);
		this.add(lblYearlyAlert);
		
		jToggleYearlyAlert = new JToggleButton("Off");
		jToggleYearlyAlert.setBounds(108, 259, 60, 23);
		this.add(jToggleYearlyAlert);	
		jToggleYearlyAlert.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setYearly(alert);
				_sidePane.update();
			}
		});
		if(alert.getYearlyBudgetCheck())
		{
			jToggleYearlyAlert.setSelected(true);
			setYearly(alert);		
		}
		
		lblExpenditureAlert = new JLabel("Expenditure Alert");
		lblExpenditureAlert.setBounds(180, 312, 144, 14);
		this.add(lblExpenditureAlert);
		
		jToggleExpenditureAlert = new JToggleButton("Off");
		jToggleExpenditureAlert.setBounds(108, 309, 60, 23);
		this.add(jToggleExpenditureAlert);	
		
		JLabel lbl_instruction = new JLabel("Alerts");
		lbl_instruction.setHorizontalAlignment(SwingConstants.CENTER);
		lbl_instruction.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		lbl_instruction.setBounds(81, 11, 186, 35);
		add(lbl_instruction);
		jToggleExpenditureAlert.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setExpenditure(alert);
				_sidePane.update();
			}
		});
		if(alert.getExpenseExceededIncomeCheck())
		{
			jToggleExpenditureAlert.setSelected(true);
			setExpenditure(alert);		
		}

	}
	
	
	public void setBalance(Alert alert)
	{
		if(jToggleBalanceAlert.isSelected())
		{
		  jToggleBalanceAlert.setText("On");
		  alert.setBalanceCheck(true);
		}
		else
		{
		  jToggleBalanceAlert.setText("Off");
		  alert.setBalanceCheck(false);
		}
	}
	
	public void setDaily(Alert alert)
	{
		if(jToggleDailyAlert.isSelected())
		{
		  jToggleDailyAlert.setText("On");
		  alert.setDailyBudgetCheck(true);
		}
		else
		{
		  jToggleDailyAlert.setText("Off");
		  alert.setDailyBudgetCheck(false);
		}
	}
	
	public void setWeekly(Alert alert)
	{
		if(jToggleWeeklyAlert.isSelected())
		{
		  jToggleWeeklyAlert.setText("On");
		  alert.setWeeklyBudgetCheck(true);
		}
		else
		{
		  jToggleWeeklyAlert.setText("Off");
		  alert.setWeeklyBudgetCheck(false);
		}
	}
	
	public void setMonthly(Alert alert)
	{
		if(jToggleMonthlyAlert.isSelected())
		{
		  jToggleMonthlyAlert.setText("On");
		  alert.setMonthlyBudgetCheck(true);
		}
		else
		{
		  jToggleMonthlyAlert.setText("Off");
		  alert.setMonthlyBudgetCheck(false);
		}
	}
	
	public void setYearly(Alert alert)
	{
		if(jToggleYearlyAlert.isSelected())
		{
		  jToggleYearlyAlert.setText("On");
		  alert.setYearlyBudgetCheck(true);
		}
		else
		{
		  jToggleYearlyAlert.setText("Off");
		  alert.setYearlyBudgetCheck(false);
		}
	}
	
	public void setExpenditure(Alert alert)
	{
		if(jToggleExpenditureAlert.isSelected())
		{
		  jToggleExpenditureAlert.setText("On");
		  alert.setExpenseExceededIncomeCheck(true);
		}
		else
		{
		  jToggleExpenditureAlert.setText("Off");
		  alert.setExpenseExceededIncomeCheck(false);
		}
	}
}
